package com.gnerv.sylvanas.framework.plugin.freemarker.tools;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultArrayAdapter;
import freemarker.template.DefaultListAdapter;
import freemarker.template.SimpleSequence;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author Gnerv LiGen
 */
@Slf4j
public class FreemarkerTools {

    private static Configuration CONFIGURATION;

    private FreemarkerTools() {
    }

    public static String generate(String path, Object parameter) {
        Template template = null;
        try {
            template = CONFIGURATION.getTemplate(path);
            return generate(template, parameter);
        } catch (IOException e) {
            log.info(e.getMessage(), e);
        }
        return "";
    }

    public static String generate(String name, String content, Object parameter, String text) {
        String generate = generate(name, content, parameter);
        generate = text + "\n" + generate;
        return generate;
    }

    public static String generate(String name, String content, Object parameter) {
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        stringTemplateLoader.putTemplate(name, content);
        CONFIGURATION.setTemplateLoader(stringTemplateLoader);
        Template template = null;
        try {
            template = CONFIGURATION.getTemplate(name);
        } catch (IOException e) {
            log.info(e.getMessage(), e);
        }
        return generate(template, parameter);
    }

    public static String generate(Template template, Object parameter, String text) {
        String generate = generate(template, parameter);
        generate = text + "\n" + generate;
        return generate;
    }

    public static String generate(Template template, Object parameter) {
        try {
            return FreeMarkerTemplateUtils.processTemplateIntoString(template, parameter);
        } catch (IOException | TemplateException e) {
            log.info(e.getMessage(), e);
        }
        return "";
    }

    public static Object getParamToList(Object collection) {
        if (collection instanceof DefaultListAdapter defaultListAdapter) {
            return defaultListAdapter.getWrappedObject();
        }
        if (collection instanceof DefaultArrayAdapter defaultArrayAdapter) {
            return defaultArrayAdapter.getWrappedObject();
        }
        if (collection instanceof SimpleSequence simpleSequence) {
            List<Object> objects = new ArrayList<>();
            for (int i = 0; i < simpleSequence.size(); i++) {
                try {
                    TemplateModel templateModel = simpleSequence.get(i);
                    objects.add(templateModel);
                } catch (TemplateModelException e) {
                    throw new RuntimeException(e);
                }
            }
            return objects;
        }
        return new ArrayList<>();
    }

    public static String getParamToString(Object param) {
        if (param instanceof TemplateScalarModel templateScalarModel) {
            try {
                return templateScalarModel.getAsString();
            } catch (TemplateModelException e) {
                log.info("获取参数失败！", e);
            }
        }
        return null;
    }

    public static void setConfiguration(Configuration configuration) {
        CONFIGURATION = configuration;
        //指定默认编码格式
        CONFIGURATION.setOutputEncoding("UTF-8");
        CONFIGURATION.setDefaultEncoding("UTF-8");
        CONFIGURATION.setEncoding(Locale.CHINA, "UTF-8");
//        try {
//            CONFIGURATION.setDirectoryForTemplateLoading(new File(SecurityUtilities.getSystemProperty("user.dir"), "templates"));
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }
}